package com.base.mine;

import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.base.commonlib.base.BaseApp;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.bean.User;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.http.utils.CallBackImpl;
import com.base.commonlib.utils.RxBus2;
import com.base.mine.databinding.ActivityModifyusernameBinding;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * 修改姓名和昵称
 * Created by weikailiang on 2020/3/25.
 */
@Route(path = "/mine/modifyusername")
public class ModifyUserNameActivity extends CBaseBarMvpActivity{
    private ActivityModifyusernameBinding mBinding;
    protected CompositeDisposable mCompositeDisposable=new CompositeDisposable();
    @Autowired
    String name;
    @Autowired
    int type;//1修改昵称2修改真实姓名
    @Autowired
    String title;
    @Override
    protected int getLayoutResource() {
        return R.layout.activity_modifyusername;
    }

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityModifyusernameBinding) mRootBinding;
        setBackNavigation();
        setBarTitle(title);
        mBinding.name.setText(name);
    }

    @Override
    protected void start() {
       mBinding.save.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               if (TextUtils.isEmpty(mBinding.name.getText().toString())){
                   showMes("请输入名称");
                   return;
               }
              if (type==1){
                  modifyName("api-auth/user/updateUserName");
              }else if (type==2){
                  modifyName("api-auth/user/updateRealName");
              }
           }
       });
    }

    private void modifyName(String meothed){
        showPb_mes("正在保存数据");
        Map<String,Object> param = new HashMap<>();
        if (type==1){
            param.put("userName",mBinding.name.getText().toString());
        }else if (type==2){
            param.put("realName",mBinding.name.getText().toString());
        }
        Disposable enqueue = RealNetWorkUtil.get().method(meothed).params(param)
                .build().enqueue(String.class, new CallBackImpl<String>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        hidePb_mes();
                        showMes(msg);
                    }

                    @Override
                    public void onNext(String o) {
                        hidePb_mes();
                        User user = BaseApp.getUser();
                        if (type == 1){
                           user.setUserName(mBinding.name.getText().toString());
                        }else if (type==2){
                            user.setRealName(mBinding.name.getText().toString());
                        }
                        BaseApp.saveUser(user);
                        RxBus2.getInstance().post(BaseApp.getUser());
                        showMes("保存成功");
                    }
                });
        mCompositeDisposable.add(enqueue);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mCompositeDisposable.dispose();
    }
}
